package kordegi.forumsystem.controller;

import io.jsonwebtoken.lang.Assert;
import kordegi.forumsystem.common.lang.Result;
import kordegi.forumsystem.entity.Comment;
import kordegi.forumsystem.service.CommentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

/**
 * @author kordegi
 * @date 2022.05.06 22:05
 * @Description
 */
@RestController
public class CommentController {
    @Autowired
    CommentService commentService;

    public Result getCommentList(@Validated @RequestBody Comment comment) {
        Map<String, Object> map = new HashMap<>();
        map.put("code", "200");
        map.put("CommentList", commentService.getAllCommentList(comment.getArticleId()));
        Assert.notNull(map);

        return Result.succ(map);
    }

    public Result deleteComment(@Validated @RequestBody Comment comment) {
        boolean flag = commentService.deleteCommentById(comment);
        if (flag == true) {
            return Result.succ(null, "删除成功");
        } else {
            return Result.fail("删除失败");
        }
    }

    public Result addCount(@Validated @RequestBody Map<String, Comment> map) {
        Comment current = map.get("current");
        Comment parent = map.get("parent");
        boolean flag = commentService.addComment(current, parent, current.getId());
        if (flag == true) {
            return Result.succ(null, "评论成功");
        } else {
            return Result.fail("评论失败");
        }
    }

    public Result updateLikeCount(@Validated @RequestBody Comment comment) {
        boolean flag = commentService.updateLikeCount(comment);
        if (flag == true) {
            return Result.succ(null);
        } else {
            return Result.fail(null);
        }
    }
}
